﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class GoodsAllotDetail
    {
        public int Add(Chain.Model.GoodsAllotDetail model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into GoodsAllotDetail(");
            builder.Append("AllotDetailAllotID,AllotDetailGoodsID,AllotDetailNumber)");
            builder.Append(" values (");
            builder.Append("@AllotDetailAllotID,@AllotDetailGoodsID,@AllotDetailNumber)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@AllotDetailAllotID", SqlDbType.Int, 4), new SqlParameter("@AllotDetailGoodsID", SqlDbType.Int, 4), new SqlParameter("@AllotDetailNumber", SqlDbType.Decimal) };
            cmdParms[0].Value = model.AllotDetailAllotID;
            cmdParms[1].Value = model.AllotDetailGoodsID;
            cmdParms[2].Value = model.AllotDetailNumber;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public DataSet AllotDetailByAllotID(int AllotID)
        {
            return DbHelperSQL.Query(string.Format("select GoodsID,Name,AllotDetailNumber  from dbo.Goods join GoodsAllotDetail on Goods.GoodsID=GoodsAllotDetail.AllotDetailGoodsID where AllotDetailAllotID={0}", AllotID));
        }

        public bool Delete(int AllotDetailID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from GoodsAllotDetail ");
            builder.Append(" where AllotDetailID=@AllotDetailID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@AllotDetailID", SqlDbType.Int, 4) };
            cmdParms[0].Value = AllotDetailID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteAllorDetail(int AllotDetailAllotID)
        {
            return (DbHelperSQL.ExecuteSql(string.Format("delete from dbo.GoodsAllotDetail  where AllotDetailAllotID={0}", AllotDetailAllotID)) > 0);
        }

        public bool DeleteList(string AllotDetailIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from GoodsAllotDetail ");
            builder.Append(" where AllotDetailID in (" + AllotDetailIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int AllotDetailID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from GoodsAllotDetail");
            builder.Append(" where AllotDetailID=@AllotDetailID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@AllotDetailID", SqlDbType.Int, 4) };
            cmdParms[0].Value = AllotDetailID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetAllotDetailByAllotID(int AllotID)
        {
            return DbHelperSQL.Query(string.Format("select  * from GoodsAllotDetail where AllotDetailAllotID={0}", AllotID));
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select AllotDetailID,AllotDetailAllotID,AllotDetailGoodsID,AllotDetailNumber ");
            builder.Append(" FROM GoodsAllotDetail ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" AllotDetailID,AllotDetailAllotID,AllotDetailGoodsID,AllotDetailNumber ");
            builder.Append(" FROM GoodsAllotDetail ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.AllotDetailID desc");
            }
            builder.Append(")AS Row, T.*  from GoodsAllotDetail T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListSP(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" select GoodsAllotDetail.*,Goods.Name,Goods.GoodsCode,Goods.GoodsType");
            builder.Append(" from GoodsAllotDetail,Goods");
            builder.Append(" where " + strWhere);
            return DbHelperSQL.Query(builder.ToString());
        }

        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("AllotDetailID", "GoodsAllotDetail");
        }

        public Chain.Model.GoodsAllotDetail GetModel(int AllotDetailID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 AllotDetailID,AllotDetailAllotID,AllotDetailGoodsID,AllotDetailNumber from GoodsAllotDetail ");
            builder.Append(" where AllotDetailID=@AllotDetailID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@AllotDetailID", SqlDbType.Int, 4) };
            cmdParms[0].Value = AllotDetailID;
            Chain.Model.GoodsAllotDetail detail = new Chain.Model.GoodsAllotDetail();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["AllotDetailID"] != null) && (set.Tables[0].Rows[0]["AllotDetailID"].ToString() != ""))
            {
                detail.AllotDetailID = int.Parse(set.Tables[0].Rows[0]["AllotDetailID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["AllotDetailAllotID"] != null) && (set.Tables[0].Rows[0]["AllotDetailAllotID"].ToString() != ""))
            {
                detail.AllotDetailAllotID = int.Parse(set.Tables[0].Rows[0]["AllotDetailAllotID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["AllotDetailGoodsID"] != null) && (set.Tables[0].Rows[0]["AllotDetailGoodsID"].ToString() != ""))
            {
                detail.AllotDetailGoodsID = int.Parse(set.Tables[0].Rows[0]["AllotDetailGoodsID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["AllotDetailNumber"] != null) && (set.Tables[0].Rows[0]["AllotDetailNumber"].ToString() != ""))
            {
                detail.AllotDetailNumber = decimal.Parse(set.Tables[0].Rows[0]["AllotDetailNumber"].ToString());
            }
            return detail;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM GoodsAllotDetail ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Update(Chain.Model.GoodsAllotDetail model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update GoodsAllotDetail set ");
            builder.Append("AllotDetailAllotID=@AllotDetailAllotID,");
            builder.Append("AllotDetailGoodsID=@AllotDetailGoodsID,");
            builder.Append("AllotDetailNumber=@AllotDetailNumber");
            builder.Append(" where AllotDetailID=@AllotDetailID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@AllotDetailAllotID", SqlDbType.Int, 4), new SqlParameter("@AllotDetailGoodsID", SqlDbType.Int, 4), new SqlParameter("@AllotDetailNumber", SqlDbType.Int, 4), new SqlParameter("@AllotDetailID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.AllotDetailAllotID;
            cmdParms[1].Value = model.AllotDetailGoodsID;
            cmdParms[2].Value = model.AllotDetailNumber;
            cmdParms[3].Value = model.AllotDetailID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool Updates(Chain.Model.GoodsAllotDetail model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update GoodsAllotDetail set ");
            builder.Append("AllotDetailAllotID=@AllotDetailAllotID,");
            builder.Append("AllotDetailGoodsID=@AllotDetailGoodsID,");
            builder.Append("AllotDetailNumber=@AllotDetailNumber");
            builder.Append(" where AllotDetailAllotID=@AllotDetailAllotID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@AllotDetailAllotID", SqlDbType.Int, 4), new SqlParameter("@AllotDetailGoodsID", SqlDbType.Int, 4), new SqlParameter("@AllotDetailNumber", SqlDbType.Int, 4), new SqlParameter("@AllotDetailAllotID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.AllotDetailAllotID;
            cmdParms[1].Value = model.AllotDetailGoodsID;
            cmdParms[2].Value = model.AllotDetailNumber;
            cmdParms[3].Value = model.AllotDetailAllotID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

